package com.example.program.service.impl;

import com.example.program.mapper.DeptMapper;
import com.example.program.mapper.EmpMapper;
import com.example.program.pojo.Dept;
import com.example.program.pojo.DeptLog;
import com.example.program.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private Deptlog adeptLog;
    @Override
    public List<Dept> list() {
        List<Dept> deptList=deptMapper.list();

        return deptList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)//运行异常报错
    public void delete(Integer id) {
try
{
    deptMapper.delete(id);
    //模拟：异常发生

    empMapper.deletea(id);

}finally {
    DeptLog deptLog=new DeptLog();
    deptLog.setCreateTime(LocalDateTime.now());
    deptLog.setDescription("执行了解散部门的操作，此时解散的是"+id+"号部门");
    //调用其他业务类中的方法
    adeptLog.loh(deptLog);
}

    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }
}
